﻿/*
    a08_attribute @ https://gitee.com/zcatt/MyDemo2020
attribute的创建和reflection
*/
using System;
using System.Reflection;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class NoteAttribute :Attribute
{
    string m_note;

    public NoteAttribute(string note)
    {
        m_note = note;
    }

    public string Note
    {
        get
        {
            return m_note;
        }
    }

    public override string ToString()
    {
        return "Note :" + m_note;
    }
}

[Note("MyClass note")]
public class MyClass
{
    [Note("methodA() note")]
    public void methodA()
    {}
}

class Program
{
    static void Main(string[] args)
    {
        Type t = typeof(MyClass);
        Console.WriteLine("Attributes for :" + t.Name);
        foreach (object attr in t.GetCustomAttributes(true))
        {
            Console.WriteLine(attr);
        }

        Console.WriteLine();

        foreach(MemberInfo m in t.GetMethods())
        {
            Console.WriteLine("Attributes for :" + m.Name);
            foreach (object attr in m.GetCustomAttributes(false))
            {
                Console.WriteLine(attr);
            }
        }

        return;
    }
}
